using System;
using System.Collections.Generic;

namespace NEnsure
{
	public class BrokenRuleList : List<BrokenRule>
	{
		public BrokenRule[] GetBrokenRulesForProperty(string propertyName)
		{
			List<BrokenRule> brokenRulesForProperty = FindAll
			(
				delegate(BrokenRule brokenRule)
				{
					return propertyName.Equals(brokenRule.PropertyName, StringComparison.InvariantCultureIgnoreCase);
				}
			);

			return brokenRulesForProperty.ToArray();
		}

		public BrokenRule[] GetBrokenRulesForRule(string ruleKey)
		{
			List<BrokenRule> brokenRulesForRule = FindAll
			(
				delegate(BrokenRule brokenRule)
				{
					return ruleKey.Equals(brokenRule.Rule.RuleKey, StringComparison.InvariantCultureIgnoreCase);
				}
			);

			return brokenRulesForRule.ToArray();
		}

		public BrokenRule[] GetBrokenRulesForRule(IBusinessRule rule)
		{
			return GetBrokenRulesForRule(rule.RuleKey);
		}

		public bool ContainsError(string errorMessage)
		{
			return Find
			(
				delegate(BrokenRule rule)
				{
					return errorMessage.Equals(rule.ErrorMessage, StringComparison.InvariantCultureIgnoreCase);
			}) != null;
		}

		public string[] GetAllErrors()
		{
			List<string> errors = new List<string>();

			foreach (BrokenRule rule in this)
			{
				errors.Add(rule.ErrorMessage);
			}

			return errors.ToArray();
		}
	}
}
